package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Like;
import java.util.List;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface LikeMapper {
    @Delete({
        "delete from `like`",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into `like` ( user_id, ",
        "invitation_id, is_liked)",
        "values (#{userId,jdbcType=INTEGER}, ",
        "#{invitationId,jdbcType=INTEGER}, #{isLiked,jdbcType=BIT})"
    })
    int insert(Like record);

    @Select({
        "select",
        "id, user_id, invitation_id, is_liked",
        "from `like`",
        "where id = #{id,jdbcType=INTEGER}"
    })
    @Results(id = "ResultMap",value = {
        @Result(column="id", property="id", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER),
        @Result(column="is_liked", property="isLiked", jdbcType=JdbcType.BIT)
    })
    Like selectByPrimaryKey(Integer id);


    @Select({
        "select",
        "id, user_id, invitation_id, is_liked",
        "from  `like`"
    })
    @ResultMap("ResultMap")
    List<Like> selectAll();

    //查询当前帖子的喜欢数量(加上.size())
    @SelectProvider(type = QueryBuilder.class,method = "likeQueryByInvitationId")
    @ResultMap("ResultMap")
    List<Like> likeQueryByInvitationId(@Param("invitationId")int invitationId);

    //查看当前用户所有喜欢的帖子的数量(加上.size())
    @SelectProvider(type = QueryBuilder.class,method = "likeQueryByUserId")
    @ResultMap("ResultMap")
    List<Like> likeQueryByUserId(@Param("userId")int userId);

    //查看当前用户对当前帖子的喜欢状态(加上.getIsLiked())
    @SelectProvider(type = QueryBuilder.class,method = "queryMyselfLike")
    @ResultMap("ResultMap")
    Like queryMyselfLike(@Param("invitationId")int invitationId,@Param("userId")int userId);


    @UpdateProvider(type = UpdateBuilder.class,method = "updateLikeStatusByUserIdAndInvitationId")
    int updateLikeStatusByUserIdAndInvitationId(@Param("invitationId")int invitationId,@Param("status")boolean status,@Param("userId")int userId);

    @Update({
        "update `like`",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "invitation_id = #{invitationId,jdbcType=INTEGER},",
          "is_liked = #{isLiked,jdbcType=BIT}",
        "where id = #{id,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Like record);
}